package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.adapter.ProjectAdapter;
import com.wnhz.work.yz.bean.AllocationBean;
import com.wnhz.work.yz.bean.SingleAllocationBean;
import com.wnhz.work.yz.bean.SingleItemBean;
import com.wnhz.work.yz.molder.EntiretyGoodsMolder2;
import com.wnhz.work.yz.molder.EntiretyMolder1;
import com.wnhz.work.yz.molder.EntiretyShoppingMolder;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---立即购买  单套配置
 */
public class SingleAllocation2Activity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "SingleAllocationActivit";
    private TextView title;
    private TextView tv_right;
    private ListView my_listView;
    private List<SingleAllocationBean> beanList = new ArrayList<>();
    private List<SingleItemBean> cateList = new ArrayList<>();

    private SingleAllocationBean allocationBean;

    private String packageId;
    private String skuId;
    private String colorId;
    private TextView tv_all_price;
    private ProjectAdapter adapter;
    private EntiretyMolder1 entiretyMolder1;
    private String goods_name;//商品的名字

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_allocation2);

        initData();
        initView();
    }

    private void initData() {
        packageId = getIntent().getStringExtra("packageId");
        skuId = getIntent().getStringExtra("skuId");
        colorId = getIntent().getStringExtra("colorId");
        if (!TextUtils.isEmpty(getIntent().getStringExtra("goods_name"))){
            goods_name = getIntent().getStringExtra("goods_name");
        }
        Log.e(TAG, "onSuccess:colorId= " + packageId);
        Log.e(TAG, "onSuccess:skuId= " + skuId);
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("单套配置");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        tv_right = (TextView) findViewById(R.id.submit);
        tv_right.setVisibility(View.VISIBLE);
        tv_right.setText("下一步");
        tv_right.setTextColor(0xff008c99);
        tv_all_price = (TextView) findViewById(R.id.tv_all_price);
        findViewById(R.id.submit).setVisibility(View.VISIBLE);
        my_listView = (ListView) findViewById(R.id.my_listView);
        upload();
    }

    private void upload() {
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("token", MyApplication.getInstance().getUserBean().getToken());
        } catch (Exception e) {
            return;
        }
        map.put("packageId", packageId);
        map.put("skuId", skuId);
        XUtil.Post(Url.PACKAGE_PACKAGELINKPRICING, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);

                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    Log.e("列表信息不能侧滑===", result);
                    if ("1".equals(res)) {
                        allocationBean = new SingleAllocationBean();
                        Gson gson = new Gson();
                        allocationBean = gson.fromJson(jsonObject.optString("info"), SingleAllocationBean.class);
                        beanList.add(allocationBean);

                        for (int i = 0; i < beanList.size(); i++) {
                            //父级列表
                            cateList = beanList.get(i).getCate_info();
                            tv_all_price.setText(beanList.get(i).getPrice());
                        }

                        adapter = new ProjectAdapter(SingleAllocation2Activity.this, cateList);
                        my_listView.setAdapter(adapter);

                    } else {
                        String info = jsonObject.optString("info");
                        MyToast(info);
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                findViewById(R.id.submit).setOnClickListener(SingleAllocation2Activity.this);
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //下一步
            case R.id.submit:
                ArrayList<SingleItemBean> arrayList = new ArrayList<>();
                for (SingleItemBean singleItemBean : cateList) {
                    ArrayList<AllocationBean> list = null;
                    for (AllocationBean allocationBean : singleItemBean.getData()) {
                        Double num = Double.parseDouble(allocationBean.getGoods_nums());
                        if (num > 0) {
                            if (list == null) {
                                list = new ArrayList<>();
                            }
                            list.add(allocationBean);
                        }
                    }
                    if (list != null) {
                        SingleItemBean itemBean = new SingleItemBean();
                        itemBean.setCate_id(singleItemBean.getCate_id());
                        itemBean.setCate_name(singleItemBean.getCate_name());
                        itemBean.setCount(singleItemBean.getCount());
                        itemBean.setData(list);
                        arrayList.add(itemBean);
                    }
                }
                EntiretyShoppingMolder molder = new EntiretyShoppingMolder();
                List<EntiretyMolder1> data = new ArrayList<>();
                for (SingleItemBean itemBean : arrayList) {
                    entiretyMolder1 = new EntiretyMolder1();
                    entiretyMolder1.setCate_id(itemBean.getCate_id());
                    List<EntiretyGoodsMolder2> data2 = new ArrayList<>();
                    for (AllocationBean allocationBean : itemBean.getData()) {
                        EntiretyGoodsMolder2 goodsMolder2 = new EntiretyGoodsMolder2();
                        goodsMolder2.setGoods_id(allocationBean.getGoods_id());
                        goodsMolder2.setGoods_nums(allocationBean.getGoods_nums());
                        goodsMolder2.setSku_id(allocationBean.getSku_id());
                        data2.add(goodsMolder2);
                    }
                    entiretyMolder1.setData(data2);
                }
                data.add(entiretyMolder1);

                String Amoney = tv_all_price.getText().toString();
                molder.setData(data);
                intent = new Intent(this, BashRoomListActivity.class);//AddCart2Activity
                intent.putExtra("money", Amoney);
                intent.putExtra("packageId", packageId);
                intent.putExtra("skuId", skuId);
                intent.putExtra("type", "0");
                intent.putExtra("molder",  molder);
                intent.putExtra("source","C");
                if (!TextUtils.isEmpty(goods_name)) {
                    intent.putExtra("goods_name",goods_name);
                }
                startActivity(intent);
                
                break;
            default:
                break;
        }
    }
}
